package coml.jiyun.hp.mvpextract.model;

import coml.jiyun.hp.mvpextract.base.BaseModel;
import coml.jiyun.hp.mvpextract.bean.Bean;
import coml.jiyun.hp.mvpextract.bean.NaviBean;
import coml.jiyun.hp.mvpextract.net.ApiService;
import coml.jiyun.hp.mvpextract.net.HttpUtil;
import coml.jiyun.hp.mvpextract.net.ResultCallBack;
import coml.jiyun.hp.mvpextract.net.ResultSubscriber;
import coml.jiyun.hp.mvpextract.net.RxUtils;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.ResourceSubscriber;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainModel extends BaseModel {

    public void getData(final ResultCallBack<NaviBean> callBack){
//                Retrofit retrofit = new Retrofit.Builder()
//                    .baseUrl(ApiService.sUrl)
//                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                    .addConverterFactory(GsonConverterFactory.create())
//                    .build();
//
//            ApiService apiService = retrofit.create(ApiService.class);
//
//            Flowable<Bean> flowable = apiService.getData2();
//
//        ResourceSubscriber<Bean> resourceSubscriber1 = flowable.subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeWith(new ResourceSubscriber<Bean>() {
//                    @Override
//                    public void onNext(Bean bean) {
//                        callBack.onSuccess(bean);
//                    }
//
//                    @Override
//                    public void onError(Throwable t) {
//                        callBack.onFail(t.toString());
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
//
//        addCompositeDisposable(resourceSubscriber1);
//
//            addCompositeDisposable(HttpUtil.getInstance()
//                    .getmWanService()
//                    .getNavi()
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribeWith(new ResourceSubscriber<NaviBean>() {
//                        @Override
//                        public void onNext(NaviBean naviBean) {
//
//                        }
//
//                        @Override
//                        public void onError(Throwable t) {
//
//                        }
//
//                        @Override
//                        public void onComplete() {
//
//                        }
//                    })
//            );
        addCompositeDisposable(
                HttpUtil.getInstance()
                        .getmWanService()
                        .getNavi()
                        .compose(RxUtils.<NaviBean>rxSchedulerHelper())//切换观察者和被观察者的线程
                        .subscribeWith(new ResultSubscriber<NaviBean>() {
                            @Override
                            public void onNext(NaviBean naviBean) {
                                callBack.onSuccess(naviBean);
                            }
                        })
        );
    }
     }
